<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>酒店搜索</title>
    <link rel="stylesheet" href="https://cache.amap.com/lbs/static/main.css"/>

    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.3&key=07b05348abcb08a58dc85263b6f8e060"></script>
    <script type="text/javascript" src="https://webapi.amap.com/demos/js/liteToolbar.js"></script>
    <script src="../../js/common/jquery-2.1.4.min.js"></script>
    <script src="../../js/common/common.js"></script>
    <script src="../../plugin/jquery.cookie.js"></script>


    <style type="text/css">
        .amap-info-content,h3{
            font-family: Microsoft Yahei;
            font-size:12px;
            min-width: 200px;
            max-width: 250px;
        }
        .amap-info-content{
            padding-right: 10px;
        }
        h3{
            margin: 0;
            font-size:13px;
        }
    </style>
</head>
<body>

<div id="mapContainer"></div>
<script>
    var map = new AMap.Map("mapContainer", {
        resizeEnable: true
    });

/********************************************************************************/
    AMap.plugin('AMap.Geocoder',function(){
        var geocoder = new AMap.Geocoder({
            city: "021"//城市，默认：“全国”
        });




      var url = location.search;//获取兼职信息id
        if (url.indexOf("?") != -1) {
            var messageid = url.substr(4);
        }

        var url1 = server + '/server/business/workMessageRest/getLonAndLatById.json';
        var json1 = {"id": messageid};
        $.post(url1, json1, function (data) {
            var str=data.data;//酒店给的经纬度
            //var str='121.518182,30.845676';//酒店给的经纬度
            var lnglatXY=str.split(",");


            var address1;
            geocoder.getAddress(lnglatXY,function(status,result){
                if(status=='complete'){
                    address1= result.regeocode.formattedAddress;
                   placeSearch(address1);
                    sessionStorage.address1 = address1;       //储存酒店地址
//                    alert(address1);
            }
            });
        });
    });
    function placeSearch(address1) {
        var placeSearch;
        AMap.service(["AMap.PlaceSearch"], function() {
            placeSearch = new AMap.PlaceSearch({ //构造地点查询类
                type:'酒店',
                city: "021" //城市
            });


            //关键字查询
            placeSearch.search(address1, function(status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    keywordSearch_CallBack(result);
                }
            });
        });
    }

    /********************************************************************************/



    var windowsArr = new Array();
    //基本地图加载

    //添加marker&infowindow
    function addmarker(i, d) {
        var lngX = d.location.getLng();
        var latY = d.location.getLat();

        //构造经纬度对象
        var ln_data = {
            lng_x:lngX,
            lat_y:latY
        };

        $.cookie('ln_data' ,JSON.stringify(ln_data),{ path:'/'});//设置cookie

        var markerOption = {
            map: map,
            icon:"https://webapi.amap.com/theme/v1.3/markers/n/mark_b"+(i+1)+".png",
            position: [lngX, latY],
            topWhenMouseOver: true

        };
        var mar = new AMap.Marker(markerOption);
        marker.push([lngX, latY]);

        var infoWindow = new AMap.InfoWindow({
            content: "<h3><font color=\"#00a6ac\">  " + (i + 1) + ". " + d.name + "</font></h3>" + createContent(d.type, d.address, d.tel),
            autoMove: true,
            offset: new AMap.Pixel(0, -30)
        });
        windowsArr.push(infoWindow);
        var aa = function(e) {
            infoWindow.open(map, mar.getPosition());
        };
        mar.on( "mouseover", aa);
    }
    //回调函数
    function keywordSearch_CallBack(data) {
        var resultStr = "";
        var poiArr = data.poiList.pois;
        var resultCount = poiArr.length;
        for (var i = 0; i < resultCount; i++) {
            resultStr += "<div id='divid" + (i + 1) + "' onmouseover='openMarkerTipById1(" + i + ",this)' onmouseout='onmouseout_MarkerStyle(" + (i + 1) + ",this)' style=\"font-size: 12px;cursor:pointer;padding:0px 0 4px 2px; border-bottom:1px solid #C1FFC1;\"><table><tr><td><img src=\"https://webapi.amap.com/images/" + (i + 1) + ".png\"></td>" + "<td><h3><font color=\"#00a6ac\">名称: " + poiArr[i].name + "</font></h3>";
            resultStr += createContent(poiArr[i].type, poiArr[i].address, poiArr[i].tel) + "</td></tr></table></div>";
            addmarker(i, poiArr[i]);
        }
        map.setFitView();
    }

    function parseStr(p){
        if(!p || p == "undefined" || p == " undefined"||p=="tel"){
            p="暂无";
        }
        return p;
    }























    function createContent(type, address, tel) {  //窗体内容
        type=parseStr(type);
        address=parseStr(address);
        tel=parseStr(tel);
        var s=[];
        s.push("地址：" +address);
        $.cookie('address',JSON.stringify(address),{path:'/'});
        s.push("电话：" +tel);
        s.push("类型：" +type);
        return '<div>'+s.join("<br>")+'</div>';
    }

    function openMarkerTipById1(pointid, thiss) {  //根据id 打开搜索结果点tip
        thiss.style.background = '#CAE1FF';
        windowsArr[pointid].open(map, marker[pointid]);
    }
    function onmouseout_MarkerStyle(pointid, thiss) { //鼠标移开后点样式恢复
        thiss.style.background = "";
    }
</script>

</body>
</html>